package com.esms.controller;

import com.esms.dao.EmployeeMapper;
import com.esms.po.Employee;
import com.esms.po.ResponsibilityMent;
import com.esms.service.DepartmentService;
import com.esms.service.EmployeeService;
import com.esms.service.IResponsibilityService;
import com.esms.utils.Message;
import com.esms.utils.Text2Html;
import com.esms.vo.EmpRespmentPages;
import com.esms.vo.EmpResponsVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.util.StringUtil;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/responsibility")
public class ResponsibilityController {

    @Autowired
    EmployeeService employeeService;

    @Autowired
    DepartmentService departmentService;
    @Autowired
    IResponsibilityService responsibilityService;
    @Autowired
    private EmployeeMapper employeeMapper = null;

    /**
     * 获取用户的部门职责
     *
     * @param httpSession
     * @return
     */
    @RequestMapping("/getDepartmentRespInfo.do")
    @ResponseBody
    public EmpRespmentPages getDepartmentRespInfo(@RequestParam(value = "page", defaultValue = "1") int pageNum,
                                                  @RequestParam(value = "limit", defaultValue = "5") int limit, HttpSession httpSession)
            throws Exception {

        int eId = (Integer) httpSession.getAttribute("employeeId");
        Employee employee = employeeMapper.selectByPrimaryKey(eId);
        String wAccount = employee.geteAccount();// 当前登录员工工号
        int dId = employee.getdId();// 当前登录员工所在部门

        //map里面的入参，要和mapper.xml文件中的条件一致
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("r_dep", dId);
        map.put("r_type", "2");

        List<EmpResponsVO> list = null;
        List<EmpResponsVO> listNew = null;
        EmpRespmentPages empRespmentPages = new EmpRespmentPages();
        List<EmpResponsVO> data = new ArrayList<>();
        try {
            //pageNum:起始页面  pageSize:每页的大小
            PageHelper.startPage(pageNum, limit);
            //查找条件，一定要紧跟在startPage后
            list = responsibilityService.getDepartmentRespInfo(map);
            for (EmpResponsVO empResponsVo : list) {
                ResponsibilityMent responsibilityMent = empResponsVo.getRespment();
                String rContent = responsibilityMent.getrContent();
                if (StringUtil.isNotEmpty(rContent)) {
                    responsibilityMent.setrContent(Text2Html.TextToHtml(rContent));//重要转换
                }
                //处理后的数据
                empResponsVo.setRespment(responsibilityMent);
                data.add(empResponsVo);
                empRespmentPages.setData(data);
                //清理原有数据，放入新的数据
                list.clear();
                list.add(empResponsVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        PageInfo pageResult = new PageInfo(list);
        //设置前台需要的数据
        empRespmentPages.setCode(0);
        empRespmentPages.setMsg("");
        empRespmentPages.setCount((int) pageResult.getTotal());
        empRespmentPages.setData(pageResult.getList());
        return empRespmentPages;
    }


    /**
     * 获取用户的岗位职责
     *
     * @param httpSession
     * @return
     */
    @RequestMapping("/getPositionRespInfo.do")
    @ResponseBody
    public EmpRespmentPages getPositionRespInfo(@RequestParam(value = "page", defaultValue = "1") int pageNum,
                                                @RequestParam(value = "limit", defaultValue = "5") int limit, HttpSession httpSession)
            throws Exception {

        int eId = (Integer) httpSession.getAttribute("employeeId");
        Employee employee = employeeMapper.selectByPrimaryKey(eId);
        String account = employee.geteAccount();// 当前登录员工工号
        int dId = employee.getdId();// 当前登录员工所在部门
        int pId = employee.getpId();// 当前登录员工所在部门

        //map里面的入参，要和mapper.xml文件中的条件一致
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("r_dep",dId);
        map.put("r_pos",pId);
        map.put("r_type", "1");
        map.put("r_byzd", account);

        List<EmpResponsVO> list = null;
        List<EmpResponsVO> listNew = null;
        EmpRespmentPages empRespmentPages = new EmpRespmentPages();
        List<EmpResponsVO> data = new ArrayList<>();
        try {
            //pageNum:起始页面  pageSize:每页的大小
            PageHelper.startPage(pageNum, limit);
            //查找条件，一定要紧跟在startPage后
            list = responsibilityService.selectPositionRespInfo(map);
            for (EmpResponsVO empResponsVo : list) {
                ResponsibilityMent responsibilityMent = empResponsVo.getRespment();
                String rContent = responsibilityMent.getrContent();
                if (StringUtil.isNotEmpty(rContent)) {
                    responsibilityMent.setrContent(Text2Html.TextToHtml(rContent));//重要转换
                }
                //处理后的数据
                empResponsVo.setRespment(responsibilityMent);
                data.add(empResponsVo);
                empRespmentPages.setData(data);
                //清理原有数据，放入新的数据
                list.clear();
                list.add(empResponsVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        PageInfo pageResult = new PageInfo(list);
        //设置前台需要的数据
        empRespmentPages.setCode(0);
        empRespmentPages.setMsg("");
        empRespmentPages.setCount((int) pageResult.getTotal());
        empRespmentPages.setData(pageResult.getList());
        return empRespmentPages;
    }

    @PostMapping("updatePosResp.do")
    @ResponseBody
    public Message updatePosResp(@RequestParam("content") String content,@RequestParam("id") Integer rId, HttpSession httpSession) {
        try {
            responsibilityService.updatePosResp(rId, content);
        } catch (Exception e) {
            return Message.fail();
        }
        return Message.success();
    }

}
